﻿using AutoMapper;
using MediatR;
using ZGLXM.Domain;
using ZGLXM.ErrorCode;
using ZGLXM.Infra.Interface;
using ZGLXM.Writh.Api.Applicantion.Command.RBAC;

namespace ZGLXM.Writh.Api.Applicantion.Handler.RBAC
{
    public class UserInsertCommandHandler:IRequestHandler<UserInsertCommand,ApiResult>
    {
        private readonly IUserRepository userRepository;
        private readonly IMapper mapper;
        private readonly ILogger<UserInsertCommandHandler> logger;

        public UserInsertCommandHandler(IUserRepository userRepository, IMapper mapper, ILogger<UserInsertCommandHandler> logger)
        {
            this.userRepository = userRepository;
            this.mapper = mapper;
            this.logger = logger;
        }

        public async Task<ApiResult> Handle(UserInsertCommand request, CancellationToken cancellationToken)
        {
            var user = mapper.Map<User>(request);
            var  result=await userRepository.InsertAsync(user);
            if(result>0)
            {
                return ApiResult.OK();
            }
            else
            {
                return ApiResult.Fail();
            }
        }
    }
}
